package ar.uade.edu.fain.ad.client.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * Servlet Filter implementation class AutenticacionFilter
 */
public class AutenticacionFilter implements Filter {

	/**
	 * Default constructor.
	 */
	public AutenticacionFilter() {
		super();
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		final HttpServletRequest httpRequest = (HttpServletRequest) request;
		if (httpRequest.getRequestURL().toString().contains("login.action")) {
			chain.doFilter(request, response);
		} else {
			final Object loginVO = httpRequest.getSession().getAttribute(
					"loginVO");
			if (loginVO == null) {
				final RequestDispatcher requestDispatcher = httpRequest
						.getRequestDispatcher("/empleado/login.action");
				requestDispatcher.forward(httpRequest, response);
			} else {
				chain.doFilter(request, response);
			}
		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
